package mp.android.WLANWidget;

import mp.android.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

public class Settings extends Activity{
	static final String TAG = "WLAN Widget Settings";

	RadioGroup groupOn;
	RadioGroup groupOff;
	CheckBox workingHrs;
	CheckBox enabled;
	
	SharedPreferences prefs;
	Editor prefEditor;

	static final int TIME_1m =    60000;
	static final int TIME_3m =   180000;
	static final int TIME_5m =   300000;
	static final int TIME_15m =  900000;
	static final int TIME_30m = 1800000;
	static final int TIME_1h =  3600000;
	static final int TIME_3h = 10800000;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.settings_layout);

		prefs = getSharedPreferences(getPackageName(),MODE_WORLD_WRITEABLE | MODE_WORLD_READABLE);
		prefEditor = prefs.edit();

		enabled = (CheckBox) findViewById(R.id.enabled);
		enabled.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				setEnabled(isChecked, false);			
			}
		});
		
		groupOn = (RadioGroup) findViewById(R.id.groupOn);
		groupOn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId){
				case R.id.radioOn15m:
					setOnTime(TIME_15m, false);
					break;
				case R.id.radioOn30m:
					setOnTime(TIME_30m, false);
					break;
				case R.id.radioOn1h:
					setOnTime(TIME_1h, false);
					break;
				case R.id.radioOn3h:
					setOnTime(TIME_3h, false);
					break;
				}
			}
		});
		
		groupOff = (RadioGroup) findViewById(R.id.groupOff);
		groupOff.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId){
				case R.id.radioOff1m:
					setOffTime(TIME_1m, false);
					break;
				case R.id.radioOff3m:
					setOffTime(TIME_3m, false);
					break;
				case R.id.radioOff5m:
					setOffTime(TIME_5m, false);
					break;
				}
			}
		});
		
		workingHrs = (CheckBox) findViewById(R.id.checkBoxWorkingHrs);
		workingHrs.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				setWorkingHrs(isChecked, false);			
			}
		});
		
		setOnTime(prefs.getInt("onTime", TIME_30m), true);
		setOffTime(prefs.getInt("offTime", TIME_3m), true);
		setWorkingHrs(prefs.getBoolean("workingHrs", true), true);
		setEnabled(prefs.getBoolean("enabled", false), true);
	}

	public void setEnabled(boolean b, boolean updateUi) {
		prefEditor.putBoolean("enabled", b);
		if (updateUi) {
			enabled.setChecked(b);
		}
	}

	public void setOnTime(int n, boolean updateUi) {
		prefEditor.putInt("onTime", n);
		if (updateUi) {
			RadioGroup btns = (RadioGroup) findViewById(R.id.groupOn);
			switch (n) {
			case TIME_15m:
				btns.check(R.id.radioOn15m);
				break;
			case TIME_30m:
				btns.check(R.id.radioOn30m);
				break;
			case TIME_1h:
				btns.check(R.id.radioOn1h);
				break;
			case TIME_3h:
				btns.check(R.id.radioOn3h);
				break;
			default:
			}
		}
	}

	public void setOffTime(int n, boolean updateUi) {
		prefEditor.putInt("offTime", n);
		if (updateUi) {
			switch (n) {
			case TIME_1m:
				groupOff.check(R.id.radioOff1m);
				break;
			case TIME_3m:
				groupOff.check(R.id.radioOff3m);
				break;
			case TIME_5m:
				groupOff.check(R.id.radioOff5m);
				break;
			}
		}
	}

	public void setWorkingHrs(boolean b, boolean updateUi) {
		prefEditor.putBoolean("workingHrs", b);
		if (updateUi) {
			workingHrs.setChecked(b);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		prefEditor.apply();
	}
}